'use strict';

/**
 * @name 选择负责人或者推荐人的弹窗
 * @author ZYF
 */

angular.module('metalabOperationApp').controller('PartnerModelCtrl', ['$rootScope', 'params', '$scope', '$uibModalInstance', 'MetaResources', 'MetaSearch', 'MetaUtils', 'MetaRemind', 'MetaFilter', function($rootScope, params, $scope, $uibModalInstance, MetaResources, MetaSearch, MetaUtils, MetaRemind, MetaFilter) {
    $scope.listCtrl = {
        filterConfig: ['area_choice'],
        filterParams: {},
        searchConfig: {
            'name_like': '姓名',
            'mobile_like': '联系方式',
            'partner_info.organization_like': '职务'
        },
        sortParams: '-created_at',
        searchParams: {},
        tableParams: {}
    };
    $scope.init = function() {
        //初始化筛选模块
        MetaFilter.initFilterModule($scope.listCtrl.filterParams, $scope.listCtrl.filterConfig, null);
        //初始化搜索
        MetaSearch.initSearchModule($scope.listCtrl.searchParams, $scope.listCtrl.searchConfig, null, $scope.changeUrl);
        //初始化表格参数
        MetaUtils.initTableModule($scope.listCtrl.tableParams, null, $scope.changeUrl, true);
        //获取数据
        $scope.getListData(1);
        if (params.data.type==="leader") {
          $scope.title = "选择负责人";
        } else {
          $scope.title = "选择推荐人";
        }
    };
    $scope.getListData = function(page) {
        var queryParams = {
            '_sort': '',
            '_range': '',
            'partnerInfo.areas': ''
        };
        var dynamicQueryParams = [];
        var urlParams = {'search': '','value': '','type': 'model'};
        MetaSearch.encodeUrlSearchParams(urlParams, $scope.listCtrl.searchParams);
        MetaUtils.buildQueryParams(page, queryParams, dynamicQueryParams, $scope.listCtrl, urlParams);
        MetaResources.user.query(
            queryParams,
            function(data, header) {
                MetaUtils.receiveListData($scope.listCtrl, null, page, data, header);
                for (var i = 0; i < data.length; i++) {
                    var queryMatrix = [
                      ['demand','demand','tags_not'],
                      ['demand','cooperation','tags'],
                      ['technology','technology','tags_not'],
                      ['technology','solution','tags']
                    ];
                    for (var j = 0; j < queryMatrix.length; j++) {
                      var amountQueryParams = {'_range': 0, 'publisher_id': $scope.listCtrl.tableParams.data[i].id, 'version': 'online'};
                      amountQueryParams[queryMatrix[j][2]] = '合作';
                      $scope.queryAmount(queryMatrix[j][0], amountQueryParams, i, queryMatrix[j][1]);
                    }
                }
            },
            function(error) {
                MetaRemind.serverError(error);
            });
    };
    $scope.queryAmount = function(module,amountQueryParams,i,key){
      MetaResources[module].query(
        amountQueryParams,
        function (date, header) {
          $scope.listCtrl.tableParams.data[i][key] = header()['x-count'];
        },
        function (error) {
          MetaRemind.serverError(error);
        }
      );
    };
    $scope.changeUrl = function() {
        $scope.getListData($scope.listCtrl.tableParams.currentPage);
    };
    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };
    $scope.selectCharge = function (index) {
      if (params.data.type==="reference") {
          $rootScope.referenceCandidate = $scope.listCtrl.tableParams.data[index].name;
          $rootScope.referenceCandidateId = $scope.listCtrl.tableParams.data[index].id;
      }else{
          $rootScope.leaderCandidate = $scope.listCtrl.tableParams.data[index].name;
          $rootScope.leaderCandidateId = $scope.listCtrl.tableParams.data[index].id;
      }
      $uibModalInstance.dismiss('cancel');
    };
    $scope.init();
}]);
